package com.mactivity.database;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.mactivity.helper.Constants;
/**
 * Author Sanjay Vasudeva
 * Class HomeActivityFunctions - Helps in cleaning up old tuples in the database(the plans and notifications in the past)
 * Also helps in bringing up the Dashboard which has all activities user is part of.
 * */
public class HomeActivityFunctions {

	/**
	 * This cleans up the database with the old records from table Notifications,GroupPlan and Individual Plans
	 */
	public void cleanDatabase(Integer I) {
		try {
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Cleaning database");

			//Couldnot pass no\null arguments in place of postparameters.Therefore passing a UserId
			//The database is cleaned up for anything which is behind the CURDATE()
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Dashboard/delete_automatic_script.php",postParameters);
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Dashboard/delete_auto_groupplan_script.php",postParameters);
			CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Dashboard/delete_auto_individualplan_script.php",postParameters);

			Log.d(Constants.LOG_DATABASE_TAG, "Database cleaned");

		} 
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}
	}

	/**
	 * This method will get the group plans to which the user has accepted to be part of from the Notifications table
	 * These are shown as GO(Group Plan by others) in the dashbboard.
	 * */
	public String getAcceptedGroupPlans(Integer I) {
		String response=null;
		try{
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//getting from the Notification table, where NoteFrom column is the UserID,NoteType=2 and TypeStatus=2
 			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Dashboard/dashboard_othergroupplan_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}

		return response;
	}

	/**
	 * This method will get the group plans to which the user has made himself from the Group Plan table
	 * These are shown as MG(My Group Plan) in the dashbboard.
	 * */
	public String getMyGroupPlans(Integer I) {
		String response=null;
		try{
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//getting from the GroupPlan table, where GroupPlanOwner column is the UserID
    		response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Dashboard/dashboard_groupplan_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}

		return response;
	}
	/**
	 * This method will get the Individual plans user has made 
	 * These are shown as Individual in the dashbboard.
	 * */
	public String getMyIndividualPlans(Integer I) {
		String response=null;
		try{
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_USERID,I.toString()));

			Log.d(Constants.LOG_DATABASE_TAG, "Connecting to database");
			//getting from the IndividualPlan table, where UserId column is the User Id
    		response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"Dashboard/dashboard_individualplan_script.php",postParameters);
 			Log.d(Constants.LOG_DATABASE_TAG, "Got data from database");
		}
		catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,"Error in http connection while connecting to the database!!"+ e.getMessage());
		}

		return response;
	}

}
